import scrapy
from scrapy.http import Response


class ZolSpider(scrapy.Spider):
  name = 'zol'
  # allowed_domains = ['desk.zol.com.cn/bizhi/9370_114202_2.html']
  start_urls = ['https://desk.zol.com.cn/bizhi/9370_114202_2.html']

  # Spider指定管道的代码
  custom_settings = {
    'ITEM_PIPELINES': {'scrapy_demo.pipelines.ImagePipeline': 300},
  }

  def parse(self, response: Response):
    img_url = response.xpath('//img[@id="bigImg"]/@src').extract()
    img_name = response.xpath('string(//h3)').extract_first()
    yield {'image_urls': img_url, 'image_name': img_name}
    next_url = response.xpath('//a[@id="pageNext"]/@href').extract_first()
    if next_url.find('.html') != -1:
      yield scrapy.Request(response.urljoin(next_url), callback=self.parse)

